(function() {

	$('.img').click(function() {
		$('.bei').show();
	});
	//

	var bei = document.getElementsByClassName('bei')[0];
	var btn = bei.getElementsByClassName('iconfont')[0];
	setTimeout(function() {
		bei.style.cssText = 'display: block;animation:bei 3s normal;';
	}, 1000);
	btn.addEventListener('click', function() {
		this.parentNode.parentNode.style.display = 'none';
		sugar();
	})

	//添加桌子的数量
	var num = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
	var mul = ['', '十', '百'];

	var seat = document.querySelector('.seat');
	var section = document.getElementsByClassName('section');
	var add = document.querySelector('.add');

	add.addEventListener('click', function(e) {;
		var tem = section[0].cloneNode(true);
		var numb;
		if(section.length < 10) {
			tem.querySelector('p').innerHTML = '第' + num[section.length] + '席';
		} else if(section.length < 20) {
			numb = section.length % 10;
			tem.querySelector('p').innerHTML = '第' + '十' + num[numb] + '席';
		} else if(section.length < 100) {
			numb = section.length % 10;
			tem.querySelector('p').innerHTML = '第' + num[parseInt(section.length.toString()[0])] + '十' + num[numb] + '席';
		}
		seat.insertBefore(tem, section[section.length - 1]);
		big();
		one();
	})

	var content = document.getElementsByClassName('content');

	//显示某个桌子
	function big() {
		for(var i = 0; i < content.length - 1; i++) {
			content[i].index = i;
			content[i].addEventListener('click', function() {
				for(var j = 0; j < content.length; j++) {
					content[j].parentNode.style.display = 'none';
				}
				this.parentNode.style.width = '100%';
				this.parentNode.style.height = '100%';
				this.parentNode.style.display = 'block';
				this.style.zIndex = '4';
				$('.home').css('display', 'none');
				$('.nv').css('display', 'none');
				$('.img').css('display', 'none');
				$('.bg').css('z-index', '3')
				if(this.index == 0) {
					this.style.transform = 'scale(2) translateY(135px)';
				} else {
					this.style.transform = 'scale(2.7) translateY(100px)';
				}
			});

		}
	}
	big();

	//恢复原来的样式
	var bg = document.getElementsByClassName('bg')[0];
	bg.addEventListener('click', function() {
		for(var i = 0; i < content.length - 1; i++) {
			content[i].index = i;
			for(var j = 0; j < content.length; j++) {
				content[j].parentNode.style.display = 'block';
			}
			if(i == 0) {
				content[i].parentNode.style.width = '50%';
				content[i].parentNode.style.height = 'auto';
				content[i].parentNode.style.margin = '0 auto';
			} else {
				content[i].parentNode.style.width = '50%';
				content[i].parentNode.style.height = 'auto';
			}

			content[i].parentNode.style.display = 'block';
			content[i].style.zIndex = 'none';
			$('.nv').css('display', 'block');
			$('.img').css('display', 'block');
			$('.bg').css('z-index', '-9999');
			$('.home').css('display', 'block');
			if(this.index == 0) {
				content[i].style.transform = 'scale(1) translateY(0)';
			} else {
				content[i].style.transform = 'scale(1) translateY(0)';
			}

		}
	});
	/********************让座位坐人******************************/
	function one() {
		var append = document.getElementsByClassName('append');
		for(var k = 0; k < append.length - 6; k++) {
			append[k].addEventListener('click', function(a) {
				var btn = true;
				a.stopPropagation();

				if(btn) {
					if(k <= stores.length) {

						this.children[0].innerHTML = '<img src=' + stores[0].src + '/>';
					} else {
						this.children[0].innerHTML = '<img src=' + stores[1].src + '/>'
					};
				} else {
					alert('该座位已有人坐，请选择其他座位');
				}

				console.log(stores.length);

			});

		}
	};
	one();

	//获取json
	var xmlhttp;
	if(window.XMLHttpRequest) {
		//				IE7 + , Firefox, Chrome, Opera, Safari 浏览器执行代码
		xmlhttp = new XMLHttpRequest();
	} else {
		// IE6, IE5 浏览器执行代码
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	//			var stores;
	xmlhttp.open('GET', '../js/img.json');
	xmlhttp.send();
	xmlhttp.addEventListener('readystatechange', function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			stores = JSON.parse(xmlhttp.responseText);
			return stores;
		};
	});

	/*************动画效果控制******************/
	$(function() {
		var music = document.getElementById('music');
		$('#musicbutton').click(function() {
			if(music.paused) {
				music.play();
				$("#musicbutton").removeClass("pause").addClass("music");
				$('.musicbutton .iconfont').css({
					'animation': 'music 4s infinite '
				});
				$('.musicbutton').css({
					'animation': 'mu 4s infinite '
				});
				$('.icon').removeClass('icon-guanbiyinle').addClass('icon-yinle');
			} else {
				music.pause();
				$('#musicbutton').removeClass('play').addClass('music');
				$('.musicbutton .iconfont').css({
					'animation': 'none'
				});
				$('.musicbutton').css({
					'animation': 'none '
				});
				$('.icon').removeClass('icon-yinle').addClass('icon-guanbiyinle');
			}
		});
	});

	/**********************糖果动画效果*************/

	function sugar() {
		var sugar = document.getElementsByClassName('sugar')[0];

		/******添加糖果***/
		for(var p = 0; p < 30; p++) {
			sugar.innerHTML += '<img src="../images/sugar.png" style="width:50px;z-index:9999;position: absolute;left:' + Math.random() * 550 + 'px ;top:' + Math.random() * (-1000) + 'px;  " > '
		};
		/******糖果掉落效果***/
		for(var k = 0; k < 30; k++) {
			$("img").eq(k).transition({
				top: Math.random() * 1 + 500,
				left:Math.random() * 1 + document.documentElement.clientWidth/2,
				rotate: Math.random() * 100 + 300 + 'deg',
				opacity: 0
			}, Math.random() * 2000 + 2000)
		}
	};
})();